﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Common;
using Dto;

namespace SqlServerDAL
{
    public class ActivityPartnerDAL
    {
        private const string WAMING = "SqlServerDAL.ActivityPartnerDAL";

        public bool Save(ActivityPartnerDto dto)
        {
            try
            {
                string sql;
                var parameters = new List<SqlParameter>
                                     {
                                         new SqlParameter("@ApActivityId", SqlDbType.Int) {Value = dto.ApActivityId},
                                         new SqlParameter("@ApUserId", SqlDbType.Int) {Value = dto.ApUserId},
                                         new SqlParameter("@ApInvitedTime", SqlDbType.DateTime)
                                             {Value = dto.ApInvitedTime},
                                         new SqlParameter("@ApActivityType", SqlDbType.Int) {Value = dto.ApActivityType},
                                         new SqlParameter("@ApState", SqlDbType.Int) {Value = dto.ApState},
                                         new SqlParameter("@ApMessage", SqlDbType.Int) {Value = dto.ApMessage}
                                     };

                if (dto.ApId == 0)
                {
                    sql =
                        @"insert into ActivityPartner(ApActivityId,ApUserId,ApInvitedTime,ApActivityType,ApState,ApMessage) values(@ApActivityId,
                        @ApUserId,@ApInvitedTime,@ApActivityType,@ApState,@ApMessage)";
                }
                else
                {
                    sql =
                        @"update ActivityPartner set ApActivityId=@ApActivityId,ApUserId=@ApUserId,ApInvitedTime=@ApInvitedTime,
                        ApActivityType=@ApActivityType,ApState=@ApState,ApMessage=@ApMessage where ApId=@ApId";
                }
                SqlHelper.ExecuteNonQuery(SqlHelper.connStr, CommandType.Text, sql, parameters.ToArray());
                return true;
            }
            catch (Exception ex)
            {
                Log.LogHandler.LogError(WAMING + ".Save(ActivityPartnerDto dto)", ex);
                return false;
            }
        }
    }
}
